<template>
  <div>
    <h1>购物车(*☻-☻*)</h1>
    <el-table
        :data="tableData"
        border
        show-summary
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="商品名称">
      </el-table-column>
      <el-table-column
          prop="buyNum"
          sortable
          label="购买数量">
        <template v-slot="scope">
          <el-input-number v-model="scope.row.buyNum" @change="handleChange(scope.row)" :min="0" :max="10" label="描述文字"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column
          prop="totalPrice"
          sortable
          label="金额">
        <template v-slot="scope">
          {{scope.row.totalPrice}}元
        </template>
      </el-table-column>
    </el-table>
    <div style="padding-left: 570px;padding-top: 15px">
      <el-button @click="addOrder" type="danger">立即结算</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "CartView",
  data(){
    return{
      tableData:[]
    }
  },methods:{
    cartList(){
      this.axios.post("http://localhost:8003/api/tb-goods/cartList").then(res=>{
        this.tableData=res.data
      })
    },handleChange(row) {
      alert(row.id)
      if(row.buyNum==0){
        this.axios.post("http://localhost:8003/api/tb-goods/delCart?id="+row.id).then(res=>{
          if(res.data=='success'){
            this.cartList();
          }
        })
      }
      else{
        this.axios.post("http://localhost:8003/api/tb-goods/updateBuyNum",row).then(res=>{
          if(res.data=='success'){
            this.cartList();
          }
        })
      }
    },addOrder(){
      this.axios.post("http://localhost:8003/api/tb-goods/addOrder").then(res=>{
        if(res.data.msg=="success"){
          this.axios.post("http://localhost:8003/api/tb-goods/toPay?outTradeNo="+res.data.outTradeNo).then(res=>{
            let win=window.open("#","_blank");
            win.document.writeln(res.data)
          })
        }
      })
    }
  },created() {
    this.cartList();
  }
}
</script>

<style scoped>

</style>